package com.ebupt.migu.music.sms.service.impl;

import com.ebupt.migu.music.approve.mapper.ApproveMapper;
import com.ebupt.migu.music.common.constants.StrategyConstants;
import com.ebupt.migu.music.common.entity.MegamarketResultObject;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.market.mapper.MarketSmsStrategyMapper;
import com.ebupt.migu.music.sms.enty.SubCompanyCampAuditNotifyParam;
import com.ebupt.migu.music.sms.service.SendToMegamarketService;
import com.ebupt.migu.music.strategy.mapper.StrategyMapper;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service(value = "SendToMegamarketService")
@Slf4j
public class SendToMegamarketServiceImpl implements SendToMegamarketService {

    @Resource
    private StrategyMapper strategyMapper;
    @Resource
    private ApproveMapper approveMapper;
    @Resource
    private MarketSmsStrategyMapper marketSmsStrategyMapper;

    @Override
    public MegamarketResultObject subCompanyCampAuditNotify(SubCompanyCampAuditNotifyParam subCompanyCampAuditNotifyParam) {
        log.info(">>>>>>>>>>>>>>subCompanyCampAuditNotifyParam:{}", subCompanyCampAuditNotifyParam.toString());
        // 挂机短信的审核回调
        if (subCompanyCampAuditNotifyParam.getCampsegId().startsWith("MS")) {
            // 大营销平台审核通过后，挂机短信策略直接进入到进行中状态。  2- 已驳回
            String status = subCompanyCampAuditNotifyParam.getAuditStatus() != 0 ? "3" : "2";
            marketSmsStrategyMapper.updateExecuteStatusByStrategyId(status, subCompanyCampAuditNotifyParam.getCampsegId());
            MegamarketResultObject megamarketResultObject = new MegamarketResultObject();
            megamarketResultObject.setCode(StatusEnum.SUCCESS.getCode());
            megamarketResultObject.setMessage(StatusEnum.SUCCESS.getMsg());
            return megamarketResultObject;
        }

        //查询出相应测策略
        Map<String, Object> strategyInfo = strategyMapper.queryByStrategyId(subCompanyCampAuditNotifyParam.getCampsegId());
        if (strategyInfo == null) {
            log.error("<<<<<<<<<<<大营销平台回调接口:找不到策略id：{}", subCompanyCampAuditNotifyParam.getCampsegId());
            throw new MyException(StatusEnum.ERROR, "找不到策略id");
        }
        log.info(">>>>>>>>>>>>>>大营销平台回调接口:开始修改审批状态{}", subCompanyCampAuditNotifyParam.getCampsegId());
        //构建审批历史类
        Map<String, Object> approverInfo = Maps.newHashMap();
        approverInfo.put("userGroupName", strategyInfo.get("groupName"));
        approverInfo.put("strategyId", subCompanyCampAuditNotifyParam.getCampsegId());
        approverInfo.put("strategyName", strategyInfo.get("strategyName"));
        approverInfo.put("strategyType", StrategyConstants.TYPE_SMS);
        approverInfo.put("creatorId", strategyInfo.get("creatorId"));
        approverInfo.put("creatorName", strategyInfo.get("creatorName"));
        approverInfo.put("creatorDepartment", strategyInfo.get("creatorDepartment"));
        approverInfo.put("creatorPhone", strategyInfo.get("creatorPhone"));
        approverInfo.put("approverId", "");
        approverInfo.put("approverName", "大营销平台");
        approverInfo.put("approveStatus", (subCompanyCampAuditNotifyParam.getAuditStatus() != 0) ? 2 : 3);
        approverInfo.put("approveDesc", "大营销平台审核：" + subCompanyCampAuditNotifyParam.getMessage());
        Map<String, Object> strParams = new HashMap<>();
        strParams.put("strategyId", subCompanyCampAuditNotifyParam.getCampsegId());
        strParams.put("executeStatus", subCompanyCampAuditNotifyParam.getAuditStatus() != 0 ? StrategyConstants.EXECUTE_UNEXECUTED : StrategyConstants.EXECUTE_REJECTED);
        try {
            //将审批历史保存
            approveMapper.createApproveByMap(approverInfo);
            strategyMapper.updateApproveInfo(strParams);
            log.info(">>>>>>>>>>>>>>大营销平台回调接口:结束修改审批状态，审批状态为{}", subCompanyCampAuditNotifyParam.getAuditStatus());
        } catch (Exception e) {
            log.error("审批失败", e);
            throw new MyException(StatusEnum.ERROR, "审批失败");
        }
        MegamarketResultObject megamarketResultObject = new MegamarketResultObject();
        megamarketResultObject.setCode(StatusEnum.SUCCESS.getCode());
        megamarketResultObject.setMessage(StatusEnum.SUCCESS.getMsg());
        return megamarketResultObject;
    }
}